#ifndef _PROCESS_POOL_HPP_
#define _PROCESS_POOL_HPP_

#include <iostream>
#include <vector>
#include <unistd.h>
#include <sys/wait.h>
#include "Task.hpp"
using namespace std;

//先描述
class Channel
{
public:
    Channel(int fd,pid_t id): _wfd(fd),_subid(id)
    {
        _name = "channel-" + to_string(_wfd) + "-" + to_string(_subid);
    }

    ~Channel()
    {

    }

    void Send(int code)
    {
        int n = write(_wfd,&code,sizeof(code));
        (void)n;
    }

    void Close()
    {
        close(_wfd);
    }

    void Wait()
    {
        pid_t rid = waitpid(_subid, nullptr, 0);
        (void)rid;
    }

    int Fd()
    {
        return _wfd;
    }

    pid_t SubId()
    {
        return _subid;
    }

    string Name()
    {
        return _name;
    }

private:
    int _wfd;
    pid_t _subid;
    string _name;
    //int _loadnum;
};

//再组织
class ChannelManager
{
public:
    ChannelManager(): _next(0)
    {

    }

    ~ChannelManager()
    {

    }

    void Insert(int wfd,pid_t subid)
    {
        _channels.emplace_back(wfd,subid);
        // Channel c(wfd,subid);
        // _channels.push_back(move(c));
    }

    Channel& Select()
    {
        auto& c = _channels[_next];
        _next++;
        _next %= _channels.size();
        return c;
    }

    void PrintChannel()
    {
        for(auto& channel : _channels)
        {
            cout << channel.Name() << endl;
        }
    }

    void CloseAll()
    {
        for(auto& channel: _channels)
        {
            channel.Close();
        }
    }

    void StopSubProcess()
    {
        for(auto& channel: _channels)
        {
            channel.Close();
            cout << "关闭: " << channel.Name() << endl;
        }
    }

    void WaitSubProcess()
    {
        for(auto& channel: _channels)
        {
            channel.Wait();
            cout << "回收: " << channel.Name() << endl;
        }
    }

    void CloseAndWait()
    {
        for(auto& channel: _channels)
        {
            channel.Close();
            cout << "关闭: " << channel.Name() << endl;
            channel.Wait();
            cout << "回收: " << channel.Name() << endl;
        }

        //解决方法1 倒着关闭
        // for(int i = _channels.size() - 1;i >= 0;i--)
        // {
        //     _channels[i].Close();
        //     cout << "关闭: " << _channels[i].Name() << endl;
        //     _channels[i].Wait();
        //     cout << "回收: " << _channels[i].Name() << endl;
        // }
    }

private:
    vector<Channel> _channels;
    int _next;
};

const int gdefaultnum = 5;

class ProcessPool
{
public:
    ProcessPool(int num): _process_num(num)
    {
        _tm.Register(PrintLog);
        _tm.Register(DownLoad);
        _tm.Register(UpLoad);
    }

    ~ProcessPool()
    {

    }

    void Work(int rfd)
    {
        while(true)
        {
            int code = 0;
            size_t n = read(rfd,&code,sizeof(code));
            if(n > 0)
            {
                if(n != sizeof(code))
                {
                    continue;
                }
                cout << "子进程[" << getpid() << "]收到一个任务码: " << code << endl;
                _tm.Execute(code);
            }
            else if(n == 0)
            {
                cout << "子进程退出" << endl;
                break;
            }
            else
            {
                cout << "读取错误" << endl;
                break;
            }
        }
    }

    bool Start()
    {
        for(int i = 0;i < _process_num;i++)
        {
            //1.创建管道
            int pipefd[2] = { 0 };
            int n = pipe(pipefd);
            if(n < 0)
            {
                return false;
            }

            //2.创建子进程
            pid_t subid = fork();
            if(subid < 0)
            {
                return false;
            }
            else if(subid == 0)
            {
                //子进程
                //关闭子进程继承的哥哥的w端
                _cm.CloseAll();

                //3.关闭不需要的文件描述符
                close(pipefd[1]);
                Work(pipefd[0]);
                close(pipefd[0]);
                exit(0);
            }
            else
            {
                //父进程
                //3.关闭不需要的文件描述符
                close(pipefd[0]);
                _cm.Insert(pipefd[1],subid);
            }
        }
        return true;
    }

    void Debug()
    {
        _cm.PrintChannel();
    }

    void Run()
    {
        //1.选择一个任务
        int taskcode = _tm.Code();
        //2.选择一个信道[子进程],负载均衡的选择一个子进程，完成任务
        auto& c = _cm.Select();
        cout << "选择了一个子进程: " << c.Name() << endl;
        //3.发送任务
        c.Send(taskcode);
        cout << "发送了一个任务码: " << taskcode << endl;
    }

    void Stop()
    {
        //关闭父进程
        //_cm.StopSubProcess();
        //回收所有的子进程
        //_cm.WaitSubProcess();

        _cm.CloseAndWait();
    }

private:
    ChannelManager _cm;
    int _process_num;
    TaskManager _tm;
};

#endif